import { createRouter, createWebHashHistory } from 'vue-router'

// const Login = () => import('../components/Login')
const Login = () =>  import(/* webpackChunkName:"login_home_welcome" */ '../components/Login')
const Welcome = () =>  import(/* webpackChunkName:"login_home_welcome" */ '../components/Welcome')
const Home = () =>  import(/* webpackChunkName:"login_home_welcome" */ '../components/Home')

// const Home = () => import('../components/Home')
// const Welcome = () => import('../components/Welcome')

// const User = () => import('../components/user/User')
// const Role = () => import('../components/power/Role')
// const Right = () => import('../components/power/Right')
const User = () =>  import(/* webpackChunkName:"User_Role_Right" */ '../components/user/User')
const Role = () =>  import(/* webpackChunkName:"User_Role_Right" */ '../components/power/Role')
const Right = () =>  import(/* webpackChunkName:"User_Role_Right" */ '../components/power/Right')

// const Category = () => import('../components/goods/Category')
const Category = () =>  import(/* webpackChunkName:"Category_Params" */ '../components/goods/Category')
// const Params = () => import('../components/goods/Param')
const Params = () =>  import(/* webpackChunkName:"Category_Params" */ '../components/goods/Param')
// const List = () => import('../components/goods/List')
const List = () =>  import(/* webpackChunkName:"List_Add" */ '../components/goods/List')
// const Add = () => import('../components/goods/Add')
const Add = () =>  import(/* webpackChunkName:"List_Add" */ '../components/goods/Add')
// const Order = () => import('../components/order/Order')
const Order = () =>  import(/* webpackChunkName:"Order_Report" */ '../components/order/Order')
// const Report = () => import('../components/report/Report')
const Report = () => import(/* webpackChunkName:"Order_Report" */ '../components/report/Report')

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', component: Login },
  {
    path: '/home',
    component: Home,
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: User },
      { path: '/roles', component: Role },
      { path: '/rights', component: Right },
      { path: '/categories', component: Category },
      { path: '/params', component: Params },
      { path: '/goods', component: List },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report },
    ]
  }
]

const router = createRouter({
  history: createWebHashHistory(),
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') return next()
  else {
    const tokenStr = window.sessionStorage.getItem('token')
    // 如果没有token,自动跳转到登录页
    if (!tokenStr) {
      return next('/login')
    }
    next()
  }
})

export default router
